package com.dfkj.wmswcs.job;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dfkj.wmswcs.common.BaseResponse;
import com.dfkj.wmswcs.mapper.WcsBarCodeMapper;
import com.dfkj.wmswcs.mapper.WmConveyTaskMapper;
import com.dfkj.wmswcs.mapper.WsTaskMapper;
import com.dfkj.wmswcs.model.domain.WmConveyTask;
import com.dfkj.wmswcs.model.domain.WsTask;
import com.dfkj.wmswcs.model.dto.WmsConveyTaskDto;
import com.dfkj.wmswcs.model.dto.WmsTaskChangeDto;
import com.dfkj.wmswcs.model.dto.WmsTaskNewDestinationDto;
import com.dfkj.wmswcs.service.WmsToWcsService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author boxyang
 * @date 2025/03/26
 * 推送任务信息
 */
@Component
@Async
@Log4j2

public class WmsToWcsTask {

    @Value("${wcs.api.url}")
    private String baseUrl;


    @Resource
    private WsTaskMapper wsTaskMapper;

    @Resource
    private WmConveyTaskMapper wmConveyTaskMapper;

    @Resource
    private WcsBarCodeMapper wcsBarCodeMapper;

    @Resource
    private WmsToWcsService wmsToWcsService;


    @Scheduled(cron = "${task.time}")
    //推送任务
    public void pushWsTask() {
        BaseResponse baseResponse = wmsToWcsService.setStackerTask();

    }


    @Scheduled(cron = "${task.time}")
    @Transactional
    //更新任务状态
    public void pushWsTaskStatus() {

        QueryWrapper<WsTask> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 2);

        List<WsTask> wsTasks = wsTaskMapper.selectList(wrapper);
        if (wsTasks.isEmpty()) {
            log.info("无需要更新任务数据");
            return;
        }

        for (WsTask wsTask : wsTasks) {
            WmsTaskChangeDto changeDto = new WmsTaskChangeDto();
            BeanUtils.copyProperties(wsTask, changeDto);
            BaseResponse baseResponse = wmsToWcsService.changeTaskStatus(changeDto);
            if (baseResponse.getCode() == 0) {
                wsTask.setStatus(1);
                wsTaskMapper.updateById(wsTask);
            }

        }

    }

    @Scheduled(cron = "${task.time}")
    @Transactional
    //推送新目的地
    public void pushStackerTaskNewDestination() {

        QueryWrapper<WsTask> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 3);

        List<WsTask> wsTasks = wsTaskMapper.selectList(wrapper);
        if (wsTasks.isEmpty()) {
            log.info("无最新更新目的地数据");
            return;
        }
        for (WsTask wsTask : wsTasks) {
            WmsTaskNewDestinationDto dto = new WmsTaskNewDestinationDto();
            BeanUtils.copyProperties(wsTask, dto);
            BaseResponse baseResponse = wmsToWcsService.setStackerTaskNewDestination(dto);
            if (baseResponse.getCode() == 0) {
                wsTask.setStatus(1);
                wsTaskMapper.updateById(wsTask);
            }


        }

    }

    @Scheduled(cron = "${task.time}")
    //推送箱式线任务
    @Transactional
    public void pushConveryTask() {

        QueryWrapper<WmConveyTask> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        List<WmConveyTask> wmConveyTasks = wmConveyTaskMapper.selectList(wrapper);

        if (wmConveyTasks.isEmpty()) {
            log.info("无最新推送箱式线任务数据");
            return;
        }
        for (WmConveyTask wmConveyTask : wmConveyTasks) {
            String location = wmConveyTask.getLocation();
            List<String> locations = Arrays.asList(location.split(","));
            WmsConveyTaskDto taskDto = new WmsConveyTaskDto();
            BeanUtils.copyProperties(wmConveyTask, taskDto);
            taskDto.setLocation(locations);
            BaseResponse baseResponse = wmsToWcsService.setConveryTask(taskDto);
            //更新状态
            if (baseResponse.getCode() == 0) {
                wmConveyTask.setStatus(1);
                wmConveyTaskMapper.updateById(wmConveyTask);
            }
        }


    }

    @Scheduled(cron = "${task.time}")
    public void pushWmsToWcsBarCode() {

        wmsToWcsService.wmsToWcsBarCode();

    }


}
